package com.ruoyi.labsafety.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.labsafety.domain.LabSafetyCheckTask;
import com.ruoyi.labsafety.service.ILabSafetyCheckFormService;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 安全检查任务Controller
 *
 * @author jun.wang
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/labsafety/task")
public class LabSafetyCheckTaskController extends BaseController {
    @Autowired
    private ILabSafetyCheckTaskService labSafetyCheckTaskService;
    @Autowired
    private ILabSafetyCheckFormService labSafetyCheckFormService;
    @Autowired
    private ILabOrgnizationService labOrgnizationService;
    /**
     * 查询安全检查任务列表
     */
    //@PreAuthorize("@ss.hasPermi('labsafety:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabSafetyCheckTask labSafetyCheckTask) {

        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        labSafetyCheckTask.setLoginUserId(userId);

        startPage();
        List<LabSafetyCheckTask> list = labSafetyCheckTaskService.selectLabSafetyCheckTaskList(labSafetyCheckTask);
        return getDataTable(list);
    }
    @GetMapping(value = "/immediatelyCheck")
    public AjaxResult getImmediatelyCheck() {
        return AjaxResult.success(labSafetyCheckFormService.selectImmediatelyCheckForm());
    }
    
    @PostMapping(value = "/immediatelyCheck")
    public AjaxResult generalImmediatelyCheck(@RequestBody LabSafetyCheckTask labSafetyCheckTask) {
        if(labSafetyCheckTask.getLabCode()!=null){
        	LabOrgnization queryLab = new LabOrgnization();
        	queryLab.setQrCode(labSafetyCheckTask.getLabCode());
        	List<LabOrgnization>  labList = labOrgnizationService.selectLabOrgnizationList(queryLab);
        	if(labList.size()==1){
        		labSafetyCheckTask.setOrgId(labList.get(0).getOrgId());
        	}else{
        		if(labList.size()>1){
        			return AjaxResult.error("错误：该实验室的编码["+labSafetyCheckTask.getLabCode()+"]存在多个.");
        		}else{
        			return AjaxResult.error("错误：该实验室编码["+labSafetyCheckTask.getLabCode()+"]不存在.");
        		}
        	}
        }
    	return toAjax(labSafetyCheckTaskService.createImmediatelyTask(labSafetyCheckTask.getInstanceId(), labSafetyCheckTask.getOrgId()));
    }
    
    /**
     * 导出安全检查任务列表
     */
    //@PreAuthorize("@ss.hasPermi('labsafety:task:export')")
    @Log(title = "安全检查任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabSafetyCheckTask labSafetyCheckTask) {
        List<LabSafetyCheckTask> list = labSafetyCheckTaskService.selectLabSafetyCheckTaskList(labSafetyCheckTask);
        ExcelUtil<LabSafetyCheckTask> util = new ExcelUtil<LabSafetyCheckTask>(LabSafetyCheckTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取安全检查任务详细信息
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:task:query')")
    @GetMapping(value = "/{instanceId}")
    public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId) {
        return AjaxResult.success(labSafetyCheckTaskService.selectLabSafetyCheckTaskById(instanceId));
    }

    /**
     * 新增安全检查任务
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:task:add')")
    @Log(title = "安全检查任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabSafetyCheckTask labSafetyCheckTask) {
        return toAjax(labSafetyCheckTaskService.insertLabSafetyCheckTask(labSafetyCheckTask));
    }

    /**
     * 修改安全检查任务
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:task:edit')")
    @Log(title = "安全检查任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabSafetyCheckTask labSafetyCheckTask) {
        String[] safetyTemplateOptArray = labSafetyCheckTask.getSafetyTemplateOptArray();
        String safetyTemplateOpt = StringUtils.join(safetyTemplateOptArray, ",");
        labSafetyCheckTask.setSafetyTemplateOpt(safetyTemplateOpt);
        labSafetyCheckTaskService.updateLabSafetyCheckTask(labSafetyCheckTask);
        return AjaxResult.success(labSafetyCheckTask);
    }

    /**
     * 新增安全检查单
     */
    @Log(title = "安全检查单", businessType = BusinessType.INSERT)
    @PostMapping("submit")
    public AjaxResult submit(@RequestBody LabSafetyCheckTask labSafetyCheckTask) {
        String[] safetyTemplateOptArray = labSafetyCheckTask.getSafetyTemplateOptArray();
        String safetyTemplateOpt = StringUtils.join(safetyTemplateOptArray, ",");
        labSafetyCheckTask.setSafetyTemplateOpt(safetyTemplateOpt);

        labSafetyCheckTaskService.submitLabSafetyCheckTask(labSafetyCheckTask);
        return AjaxResult.success(labSafetyCheckTask);
    }

    /**
     * 删除安全检查任务
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:task:remove')")
    @Log(title = "安全检查任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{instanceIds}")
    public AjaxResult remove(@PathVariable Long[] instanceIds) {
        return toAjax(labSafetyCheckTaskService.deleteLabSafetyCheckTaskByIds(instanceIds));
    }

    /**
     * 修改安全检查任务
     */
//    @PreAuthorize("@ss.hasPermi('labsafety:task:edit')")
    @Log(title = "安全检查任务", businessType = BusinessType.UPDATE)
    @PutMapping("/sign/{instanceId}")
    public AjaxResult sign(@PathVariable("instanceId") Long instanceId) {
        return toAjax(labSafetyCheckTaskService.signLabSafetyCheckTask(instanceId));
    }
}
